<template>
  <div class="product-list">
    <h2>商品列表</h2>
    <div class="products-grid">
      <div
        v-for="product in products"
        :key="product.id"
        class="product-card"
        :class="{ 'out-of-stock': product.stock === 0 }"
      >
        <div class="product-image">
          <img :src="product.image" :alt="product.name" />
          <div v-if="product.stock === 0" class="stock-overlay">
            <span>缺货</span>
          </div>
        </div>

        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="description">{{ product.description }}</p>
          <p class="price">¥{{ product.price.toFixed(2) }}</p>

          <div class="stock-info">
            <span class="stock-label">库存:</span>
            <span class="stock-count" :class="{ 'low-stock': product.stock <= 5 && product.stock > 0 }">
              {{ product.stock }}
            </span>
          </div>

          <div class="quantity-selector" v-if="product.stock > 0">
            <label>数量:</label>
            <div class="quantity-controls">
              <button
                @click="decreaseQuantity(product.id)"
                :disabled="getQuantity(product.id) <= 1"
                class="quantity-btn"
              >
                -
              </button>
              <span class="quantity-display">{{ getQuantity(product.id) }}</span>
              <button
                @click="increaseQuantity(product.id)"
                :disabled="getQuantity(product.id) >= product.stock"
                class="quantity-btn"
              >
                +
              </button>
            </div>
          </div>

          <button
            @click="handleReserve(product)"
            :disabled="product.stock === 0 || isReserving"
            class="reserve-btn"
            :class="{
              'disabled': product.stock === 0,
              'loading': isReserving && reservingProductId === product.id
            }"
          >
            <span v-if="isReserving && reservingProductId === product.id" class="btn-spinner"></span>
            {{
              isReserving && reservingProductId === product.id
                ? '预定中...'
                : product.stock === 0
                  ? '缺货'
                  : '立即预定'
            }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

defineProps(['products'])
const emit = defineEmits(['reserve-product'])

// 每个商品的选择数量
const quantities = ref({})

// 预定状态
const isReserving = ref(false)
const reservingProductId = ref(null)

// 获取商品数量
const getQuantity = (productId) => {
  return quantities.value[productId] || 1
}

// 增加数量
const increaseQuantity = (productId) => {
  const current = quantities.value[productId] || 1
  quantities.value[productId] = current + 1
}

// 减少数量
const decreaseQuantity = (productId) => {
  const current = quantities.value[productId] || 1
  if (current > 1) {
    quantities.value[productId] = current - 1
  }
}

// 处理预定
const handleReserve = async (product) => {
  if (isReserving.value) return

  isReserving.value = true
  reservingProductId.value = product.id

  try {
    // 添加一点延迟来显示加载状态
    await new Promise(resolve => setTimeout(resolve, 300))

    const quantity = getQuantity(product.id)
    emit('reserve-product', { product, quantity })
  } finally {
    isReserving.value = false
    reservingProductId.value = null
  }
}
</script>

<style scoped>
.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 12px;
  overflow: hidden;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s, box-shadow 0.2s;
}

.product-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.product-card.out-of-stock {
  opacity: 0.6;
  filter: grayscale(0.5);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.stock-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.2rem;
  font-weight: bold;
}

.product-info {
  padding: 1rem;
  text-align: left;
}

.product-info h3 {
  margin: 0 0 0.5rem 0;
  font-size: 1.1rem;
  color: #333;
}

.description {
  color: #666;
  font-size: 0.9rem;
  margin: 0 0 0.5rem 0;
  line-height: 1.4;
}

.price {
  font-size: 1.3rem;
  font-weight: bold;
  color: #e74c3c;
  margin: 0.5rem 0;
}

.stock-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

.stock-label {
  color: #666;
}

.stock-count {
  font-weight: bold;
  color: #28a745;
}

.stock-count.low-stock {
  color: #ffc107;
}

.quantity-selector {
  margin: 1rem 0;
}

.quantity-selector label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
  color: #666;
}

.quantity-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.quantity-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.quantity-btn:hover:not(:disabled) {
  background: #f8f9fa;
}

.quantity-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity-display {
  min-width: 40px;
  text-align: center;
  font-weight: bold;
}

.reserve-btn {
  width: 100%;
  background: #007bff;
  color: white;
  border: none;
  padding: 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: bold;
  transition: background-color 0.2s;
}

.reserve-btn:hover:not(.disabled) {
  background: #0056b3;
}

.reserve-btn.disabled {
  background: #6c757d;
  cursor: not-allowed;
}

.reserve-btn.loading {
  background: #0056b3;
  cursor: wait;
  position: relative;
}

.btn-spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-right: 0.5rem;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 商品卡片进入动画 */
.product-card {
  animation: fadeInUp 0.6s ease-out;
}

.product-card:nth-child(1) { animation-delay: 0.1s; }
.product-card:nth-child(2) { animation-delay: 0.2s; }
.product-card:nth-child(3) { animation-delay: 0.3s; }
.product-card:nth-child(4) { animation-delay: 0.4s; }
.product-card:nth-child(5) { animation-delay: 0.5s; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>